﻿using Furion.DynamicApiController;
using FurionApi.Demo.Application.Cache.Services;
using FurionApi.Demo.Application.System.Dtos;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace FurionApi.Demo.Application
{
    /// <summary>
    /// 系统服务接口
    /// </summary>
    [ApiDescriptionSettings(KeepVerb = true, SplitCamelCase = false)]
    public class SystemAppService : IDynamicApiController
    {
        private readonly ISystemService _systemService;
        private readonly ISysCacheService _sysCacheService;

        public SystemAppService(ISystemService systemService, ISysCacheService sysCacheService)
        {
            _systemService = systemService;
            this._sysCacheService = sysCacheService;
        }

        /// <summary>
        /// 获取系统描述
        /// </summary>
        /// <returns></returns>
        public string GetDescription()
        {
            return _systemService.GetDescription();
        }
        /// <summary>
        /// 测试异常
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        public int TestException(int i, int j)
        {
            return i / j;
        }

        public async Task<string> GetCacheTimeAsync()
        {
            var time = DateTimeOffset.Now.ToString();
            string time_absolute = await _sysCacheService.GetAsync<string>("time_absolute");
            if (time_absolute == null)
            {
                time_absolute = time;
                await _sysCacheService.SetAsync("time_absolute", time_absolute, 3, Cache.Enum.TimeOutType.Absolute);//绝对过期
            }
            var time_sliding = await _sysCacheService.GetAsync<string>("time_sliding");
            if (time_sliding == null)
            {
                time_sliding = time;
                await _sysCacheService.SetAsync("time_sliding", time_sliding, 3, Cache.Enum.TimeOutType.Sliding);//相对过期
            }

            var time_forever = await _sysCacheService.GetAsync<string>("time_forever");
            if (time_forever == null)
            {
                time_forever = time;
                await _sysCacheService.SetAsync("time_forever", time_forever); //永不过期
            }

            return $"time_absolute:{time_absolute}  time_sliding:{time_sliding}  time_forever:{time_forever}";

        }

        public async Task<TestInputDto> TestDtoVAsync([FromBody] TestInputDto inputDto)
        {
            return await Task.FromResult(inputDto);
        }

    }
}